package fr.noe.amicaleloisirs.concours.business.reloadExisting;

import java.util.ArrayList;
import java.util.List;

import fr.noe.amicaleloisirs.concours.business.Concours;
import fr.noe.amicaleloisirs.concours.business.Match;
import fr.noe.amicaleloisirs.concours.business.Team;
import fr.noe.amicaleloisirs.concours.utils.Logger;

/**
 * Classe à utiliser lors d'un chargement d'un concours sauvegardé. Il permet de
 * re créer l'instance métier du concours existant.
 * 
 * @author Sébastien PREVOST
 * 
 */
public class ConcoursExistant extends Concours {

	public ConcoursExistant(String date, List<Team> teams, int tour,
			List<MatchExistant> matchs) {
		super(date);
		this.teams = teams;
		this.currentTour = getTourFromInt(tour);
		this.matches = retrieveMatch(matchs);
	}
	
	private static void log(String text) {
		Logger.get().logWithClass("ConcoursExistant", text);
	}

	private List<Match> retrieveMatch(List<MatchExistant> matchs) {
		List<Match> res = new ArrayList<Match>();
		for (MatchExistant me : matchs) {
			res.add(retrieveMatch(me));
		}
		return res;
	}

	private Team getTeamById(int id) {
		for (int i = 0; i < teams.size(); i++) {
			Team t = teams.get(i);
			if (t.getTeamId() == id)
				return t;
		}
		return null;
	}

	private Match retrieveMatch(MatchExistant me) {
		Team t1 = null;
		int t1int = me.getT1();
		if (t1int != -1)
			t1 = getTeamById(t1int);

		Team t2 = null;
		int t2int = me.getT2();
		if (t2int != -1)
			t2 = getTeamById(t2int);

		return new Match(t1, t2);
	}
}